package com.example.memosystem.service.impl;

import com.example.memosystem.mapper.MemoMapper;
import com.example.memosystem.model.Memo;
import com.example.memosystem.service.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemoServiceImpl implements MemoService {
    
    @Autowired
    private MemoMapper memoMapper;

    @Override
    public boolean addMemo(Memo memo) {
        Date now = new Date();
        memo.setCreateTime(now);
        memo.setUpdateTime(now);
        return memoMapper.insertMemo(memo) > 0;
    }

    @Override
    public boolean updateMemo(Memo memo) {
        memo.setUpdateTime(new Date());
        return memoMapper.updateMemo(memo) > 0;
    }

    @Override
    public boolean deleteMemo(Integer id, Integer userId) {
        return memoMapper.deleteMemo(id, userId) > 0;
    }

    @Override
    public boolean moveToTrash(Integer id, Integer userId) {
        return memoMapper.moveToTrash(id, userId) > 0;
    }

    @Override
    public boolean restoreMemo(Integer id, Integer userId) {
        return memoMapper.restoreMemo(id, userId) > 0;
    }

    @Override
    public boolean emptyTrash(Integer userId) {
        return memoMapper.emptyTrash(userId) >= 0;
    }

    @Override
    public Memo findById(Integer id, Integer userId) {
        return memoMapper.findById(id, userId);
    }

    @Override
    public List<Memo> findAllByUserId(Integer userId) {
        return memoMapper.findAllByUserId(userId);
    }

    @Override
    public List<Memo> searchByTitle(Integer userId, String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return findAllByUserId(userId);
        }
        return memoMapper.searchByTitle(userId, keyword.trim());
    }

    @Override
    public List<Memo> findByCategoryId(Integer userId, Integer categoryId) {
        return memoMapper.findByCategoryId(userId, categoryId);
    }

    @Override
    public List<Memo> findTrashedMemos(Integer userId) {
        return memoMapper.findTrashedMemos(userId);
    }
}